package org.leon.service.impl;

import org.leon.pojo.Article;
import org.leon.repository.ArticleRepository;
import org.leon.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    private ArticleRepository articleRepository;

    private final static int PAGE_SIZE = 2;

    @Autowired
    public ArticleServiceImpl(ArticleRepository articleRepository) {
        this.articleRepository = articleRepository;
    }

    @Override
    public Page<Article> findArticleByPage(int pageNum) {
        PageRequest pageInfo = PageRequest.of(pageNum - 1, PAGE_SIZE, Sort.by("id"));
        Page<Article> articlePage = articleRepository.findAll(pageInfo);
        return articlePage;
    }
}
